package com.fr.web.core.service;

import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.file.ConfigManager;
import com.fr.util.Utils;
import com.fr.web.core.WebUtils;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResourceService extends NoSessionIDOPService
{
  private static ResourceService resourceService = null;
  public static final String RESOURCE_ENCODER = "GBK";

  public static ResourceService getInstance()
  {
    if (resourceService == null)
      resourceService = new ResourceService();
    return resourceService;
  }

  public boolean accept(String paramString)
  {
    return paramString.equals("resource");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "resource");
    paramHttpServletResponse.setHeader("Cache-Control", "max-age=86400");
    paramHttpServletResponse.setDateHeader("Expires", System.currentTimeMillis() + 86400000L);
    String str2 = setResourceContentType(str1, paramHttpServletResponse);
    try
    {
      if (str2 == "text")
      {
        TemplateUtils.dealWithTemplate(str1, paramHttpServletResponse, WebUtils.createTemplateMap(paramHttpServletRequest));
      }
      else
      {
        if ((!(str1.startsWith("/"))) && (!(str1.startsWith("\\"))))
          str1 = File.separator + str1;
        InputStream localInputStream = null;
        if (localInputStream == null)
          localInputStream = BaseUtils.readResource(str1);
        if (localInputStream != null)
        {
          ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
          try
          {
            Utils.copyBinaryTo(localInputStream, localServletOutputStream);
            localServletOutputStream.flush();
            localServletOutputStream.close();
          }
          catch (Exception localException2)
          {
          }
          localInputStream.close();
        }
      }
    }
    catch (Exception localException1)
    {
      FRContext.getLogger().log(Level.WARNING, str1, localException1);
    }
  }

  public static String setResourceContentType(String paramString, HttpServletResponse paramHttpServletResponse)
  {
    String str1 = paramString.toLowerCase();
    String str2 = FRContext.getConfigManager().getServerCharset();
    if (str1.endsWith(".png"))
    {
      paramHttpServletResponse.setContentType("image/png");
      return "image";
    }
    if (str1.endsWith(".gif"))
    {
      paramHttpServletResponse.setContentType("image/gif");
      return "image";
    }
    if (str1.endsWith(".jpg"))
    {
      paramHttpServletResponse.setContentType("image/jpeg");
      return "image";
    }
    if (str1.endsWith(".js"))
    {
      paramHttpServletResponse.setContentType("text/javascript;charset=" + str2);
      return "text";
    }
    if (str1.endsWith(".css"))
    {
      paramHttpServletResponse.setContentType("text/css;charset=" + str2);
      return "text";
    }
    if (str1.endsWith(".xml"))
    {
      paramHttpServletResponse.setContentType("text/xml;charset=" + str2);
      return "text";
    }
    if (str1.endsWith(".swf"))
    {
      paramHttpServletResponse.setContentType("application/x-shockwave-flash");
    }
    else if (str1.endsWith(".jnlp"))
    {
      paramHttpServletResponse.setContentType("application/x-java-jnlp-file");
    }
    else if (str1.endsWith(".xls"))
    {
      paramHttpServletResponse.setContentType("application/vnd.ms-excel");
    }
    else if (str1.endsWith(".exe"))
    {
      paramHttpServletResponse.setContentType("application/x-msdownload");
      paramHttpServletResponse.setHeader("extension", "exe");
      paramHttpServletResponse.setHeader("Content-disposition", "attachment; filename=AcrobatReader.exe");
    }
    else
    {
      paramHttpServletResponse.setContentType("text/html;charset=" + str2);
      return "text";
    }
    return "other";
  }
}